package com.zhl.dao;

import com.zhl.dao.dasedao.BaseDao;
import com.zhl.model.Meeting;
import com.zhl.utils.JdbcUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class MeetingDao extends BaseDao<Meeting> {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JdbcUtils.getDataSource());

    public MeetingDao() {
        super(Meeting.class);
    }

    public List<Meeting> findAllMeeting(String title,Integer status){
        List<Meeting> meetingList = null;
        if (title == null && status ==null){
            String sql = "select * from meeting";
            try {
                meetingList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class));
            }catch (Exception e){
                e.printStackTrace();
            }
        }else if (title != null && status ==null){
            String sql = "select * from meeting where title like '%' ? '%'";
            try {
                meetingList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class),title);
            }catch (Exception e){
                e.printStackTrace();
            }
        } else if (title == null && status !=null){
            String sql = "select * from meeting where status = ?";
            try {
                meetingList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class),status);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        else {
            String sql = "select * from meeting where title like '%' ? '%' and status = ?";
            try {
                meetingList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Meeting.class),title,status);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return meetingList;
    }

    public Map<String,Object> findMeetingById(Integer id){
        Map<String,Object> map = null;
        String sql = "select m.*,(select count(u_id) from meeting_join where c_id = ?) as count " +
                "from meeting m where m.id = ?";
        try {
            map = jdbcTemplate.queryForMap(sql, id, id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    public Meeting findJoinById(Integer uId,Integer mId){
        Meeting meeting = null;
        String sql = "select * from meeting_join where u_id = ? and c_id = ?";
        try {
            meeting = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Meeting.class), uId, mId);
        }catch (Exception e){
            return null;
        }
        return meeting;
    }

    public Meeting deleteJoinById(Integer uId,Integer mId){
        Meeting meeting = null;
        String sql = "delete from meeting_join where u_id = ? and c_id = ?";
        try {
            meeting = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Meeting.class), uId, mId);
        }catch (Exception e){
            e.printStackTrace();
        }
        return meeting;
    }

    public String findUserBymId(Integer mId){
        String users = null;
        String sql = "select make_user from meeting where id = ?";
        try {
            users = jdbcTemplate.queryForObject(sql,String.class, mId);
        }catch (Exception e){
            return null;
        }
        return users;
    }
}
